Skip to content

Fix codegen for non-discriminated anyOf unions with mixed types#171

Merged
daweifeng-replit merged 2 commits intomainfrom
dawei/fix-anyof
Feb 4, 2026
Merged

Fix codegen for non-discriminated anyOf unions with mixed types#171
daweifeng-replit merged 2 commits intomainfrom
dawei/fix-anyof

Conversation

@daweifeng-replit
Copy link
Contributor

Why

The encoder generation for TypedDict inputs produces malformed Python code when handling anyOf unions containing mixed types like [object, string, array].

Before

 return (
     encode_...AnyOf_0(x)
     x
      if isinstance(x, str) else
     encode_str(x)
 )

After

  return (
      encode_...AnyOf_0(x) if isinstance(x, dict) else
      x if isinstance(x, str) else
      list(x)
  )

What changed

  • Collect (type_check, encoder_expr) pairs for each union member
  • Build a proper ternary chain with isinstance checks
  • Handle primitive array items by returning list(x) instead of undefined encoder calls

Test plan

CI

@daweifeng-replit daweifeng-replit requested a review from a team as a code owner February 4, 2026 18:42
@daweifeng-replit daweifeng-replit requested review from Monkatraz and removed request for a team February 4, 2026 18:42
@daweifeng-replit daweifeng-replit added the bug Something isn't working label Feb 4, 2026
@daweifeng-replit daweifeng-replit merged commit f216da6 into main Feb 4, 2026
3 checks passed
@daweifeng-replit daweifeng-replit deleted the dawei/fix-anyof branch February 4, 2026 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants